package com.jqlin.mybatis.util;

import java.util.List;


public class PageControl<T> {
	

    private static int PAGESIZE = 20;
    /**
     * 每页对象数
     */
    private int pageSize = PAGESIZE;
    /**
     *页数
     */
    private int pageCount = 0;
    /**
     * 对象数
     */
    private int count = 0;
    /**
     * 当前页
     */
    private int currentPage = 1;
    /**
     * 当前页开始的记录的位置
     */
    private int begin = 0;
    /**
     * 当前页结束的记录的位置
     */
    private int end = 0;
   
    private String appendParams = "";
    /**
     * 当前快近所在页面
     */
    private int currentSkip = 1;
    private int skipSize = 5;
    /**
     * 结果
     */
    private List<T> list = null;
    public PageControl() {
    }
    
    public PageControl(int defaultPageSize) {
        this.pageSize = defaultPageSize;
    }

    public PageControl(int defaultPageSize, int page) {
        this.pageSize = defaultPageSize;
        this.currentPage = page;
    }

    /**
     * 返回当前页
     * @return 当前页
     */
    public int getCurrentPage() {
        return currentPage;
    }

    /**
     * 得到页数
     * @return　页数
     */
    public int getPageCount() {
        return pageCount;
    }

    /**
     * 得到每页记录条数
     * @return 每页记录条数
     */
    public int getPageSize() {
        return this.pageSize;
    }

    /**
     * 得到记录条数
     * @return 记录条数
     */
    public int getCount() {
        return count;
    }

    /**
     * 得到当前页的记录开始位置
     * @return 开始位置
     */
    public int getBegin() {
        return begin;
    }

    /**
     * 得到记录的结束位置
     * @return 结束位置
     */
    public int getEnd() {
        return end;
    }

   
    public String getAppendParams() {
        return appendParams;
    }

    /**
     * 设置记录条数
     *
     * @param totalCount int
     */
    public void setCount(int totalCount) {
        if (totalCount <= 0) {
            return;
        }
        this.count = totalCount;
        this.pageCount = totalCount / pageSize + ((totalCount % pageSize == 0) ? 0 : 1);
        if (currentPage > pageCount) {
            currentPage = pageCount;
        }
        if (currentPage <= 0) {
            currentPage = 1;
        }

        begin = (currentPage - 1) * pageSize;
        end = currentPage * pageSize;
        if (end >= totalCount) {
            end = totalCount;
        }

        currentSkip = (currentPage / skipSize) * skipSize + 1;
        if (currentPage % skipSize == 0) {
            currentSkip = currentSkip - skipSize;
        }

    }

    /**
     * 是否可以到第一页
     *
     * @return boolean
     */
    public boolean getCanGoFirst() {
        return (this.currentPage > 1);
    }

    /**
     * 是否可以到前一页
     *
     * @return boolean
     */
    public boolean getCanGoPrevious() {
        return (this.currentPage > 1);
    }

    /**
     * 是否可以到下一页
     *
     * @return boolean
     */
    public boolean getCanGoNext() {
        return (this.currentPage < this.pageCount);
    }

    /**
     * 是否可以到最后一页
     *
     * @return boolean
     */
    public boolean getCanGoLast() {
        return (this.currentPage < this.pageCount);
    }

    /**
     * 得到前一页页码
     *
     * @return int
     */
    public int getPrevious() {
        if (this.currentPage > 1) {
            return this.currentPage - 1;
        } else {
            return 1;
        }
    }

    /**
     * 得到下一页页码
     *
     * @return int
     */
    public int getNext() {
        if (this.currentPage < this.pageCount) {
            return this.currentPage + 1;
        } else {
            return this.pageCount;
        }
    }

    /**
     * 填充对象
     *
     * @param objectList List
     */
    public void setAll(List<T> objectList) {
        if (objectList != null) {
            this.setCount(objectList.size());
            list = objectList.subList(this.getBegin(), this.getEnd());
        } else {
            this.setCount(0);
        }
    }

    /**
     *
     * @param list List
     */
    public void setList(List<T> list) {
        this.list = list;
    }

    public void setAppendParams(String appendParams) {
        this.appendParams = appendParams;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    /**
     * 是否可以向前快进
     *
     * @return boolean
     */
    public boolean getCanSkipPrevious() {
        return getSkipPrevious() > 0;
    }

    /**
     * 得到向前快近的页码
     *
     * @return int
     */
    public int getSkipPrevious() {
        return this.currentSkip - skipSize;
    }

    /**
     * 是否可以向后快进
     *
     * @return boolean
     */
    public boolean getCanSkipNext() {
        return (getSkipNext() <= this.pageCount);
    }

    /**
     * 得到向后快近的页码
     *
     * @return int
     */
    public int getSkipNext() {
        return this.currentSkip + skipSize;
    }

    /**
     * 得到当前显示的页码
     *
     * @return int[]
     */
    public int[] getCurrentSkipPageNumbers() {
        int n = skipSize;
        if (currentSkip + skipSize > pageCount) {
            n = pageCount - currentSkip + 1;
        }
        int[] Result = new int[n];
        for (int i = 0; i < n; i++) {
            Result[i] = currentSkip + i;
        }
        return Result;
    }

    public void reCount(int page, int count) {
        this.currentPage = page;
        this.setCount(count);
    }

	/**
	 * @return the currentSkip
	 */
	public int getCurrentSkip() {
		return currentSkip;
	}

	/**
	 * @param currentSkip the currentSkip to set
	 */
	public void setCurrentSkip(int currentSkip) {
		this.currentSkip = currentSkip;
	}

	/**
	 * @return the skipSize
	 */
	public int getSkipSize() {
		return skipSize;
	}

	/**
	 * @param skipSize the skipSize to set
	 */
	public void setSkipSize(int skipSize) {
		this.skipSize = skipSize;
	}

	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @param pageCount the pageCount to set
	 */
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	/**
	 * @param begin the begin to set
	 */
	public void setBegin(int begin) {
		this.begin = begin;
	}

	/**
	 * @param end the end to set
	 */
	public void setEnd(int end) {
		this.end = end;
	}
    
	 public List<T> getList() {

	        return list;
	    }

}
